<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
</head>
<body>
<script th:inline="javascript">
[# th:each="field : ${fieldsInParams}"]
    function initCheckChange_[(${field.name})]() {
        // 函数宏控件在进入页面时，必进行计算
        // var curFormId = getCurFormUtil().get();
        var curFormId = '[(${formName})]';
        var oldValue_[(${field.name})] = "cws-65536"; // 一个不存在的值
        if (fo("[(${field.name})]", curFormId)) { // 防止此控件也是SQL控件，并且此时还不存在
            oldValue_[(${field.name})] = fo("[(${field.name})]", curFormId).value;
        }
        var sint = setInterval(function(){
            if (o(curFormId)) {
                if (fo("[(${field.name})]", curFormId)) {
                    if (oldValue_[(${field.name})] != fo("[(${field.name})]", curFormId).value) {
                        oldValue_[(${field.name})] = fo("[(${field.name})]", curFormId).value;
                        onFormulaCtlRelateFieldChange_[(${fieldName})]();
                    }
                } else {
                    // 使关闭抽屉时，能够销毁setInterval，否则仅管前端removeScript了，但检测代码仍会运行
                    window.clearInterval(sint);
                    console.log('macro_formula_ctl_js clearInterval [(${field.name})]');
                }
            }
        }, 200);
        // 当菜单项不启用缓存时，只能通过如下方法才能清除interval
        getCurFormUtil().addInterval(sint, '[(${formName})]');
    }
    initCheckChange_[(${field.name})]();
[/]

// 取得本表单中相应的值
function getFieldVal(fieldName) {
    if (findObj(fieldName)) {
        return findObj(fieldName).value;
    }
}

function onFormulaCtlRelateFieldChange_[(${fieldName})]() {
    var params = '[(${params})]'; // 必须得用单引号，因为可能有些参数会是字符串，带有双引号
    var formulaStr = '';
    try {
        formulaStr = '#[(${code})](' + [(${arguments})] + ')';
    }
    catch (e) {}

    var ajaxData = {
        formula: formulaStr,
    };
    ajaxPost('/visual/formula/doFormula', ajaxData).then((data) => {
		console.log('data', data);
        if (data.ret=="1") {
            var obj = findObj("[(${fieldName})]");
            if (obj) {
                $(obj).val(data.value);

                setTimeout(function () {
                    if(findObj("[(${fieldName})]_show")) {
                        findObj("[(${fieldName})]_show").innerHTML = data.value;
                    }
                }, 500);

                var frm = getCurForm();

                // 删除原来的验证，否则会因为原验证中存储的对象不存在而导致验证失效
                var formObj = LiveValidationForm.getInstance(frm);
                if (formObj) {
                    formObj.removeFieldByName('[(${fieldName})]');
                }

                [(${checkFieldJs})]
            }

            try {
                initCalculator();
            }
            catch(e) {}
        }
        else {
            console.error(data.msg);
            myMsg(data.msg, 'warning');
        }
	});
}

[# th:if="${pageType ne 'flowShow' && pageType ne 'show'}"]
    onFormulaCtlRelateFieldChange_[(${fieldName})]();
[/]
</script>
</body>
</html>